/*
  base2 - copyright 2007-2009, Dean Edwards
  http://code.google.com/p/base2/
  http://www.opensource.org/licenses/mit-license.php

  Contributors:
    Doeke Zanstra
*/

// timestamp: Wed, 30 Sep 2009 08:28:10

new function(_no_shrink_) { ///////////////  BEGIN: CLOSURE  ///////////////

// =========================================================================
// media/header.js
// =========================================================================

/*@cc_on @*/

new base2.Package(this);

eval(this.imports);

var _JAVA_ENABLED = detect("(java)");

var _MEDIA_TYPE = /^(audio|video)\/[\w-]+$/;

var _PROBABLY = {
  "audio/mp3":         1,
  "video/mp4":         1,
  "video/quicktime":   1,
  "video/x-flv":       1
};

var _MAYBE = {
  "audio/ogg":         1,
  "video/ogg":         1
};

var _USE_QT_PLAYER    = /(aac|mov|mp3|mp4)$/i,
    _USE_FLASH_PLAYER = /(aac|flv|mov|mp3|mp4)$/i,
    _USE_OGG_PLAYER   = /og[gv]$/i;

var _TRIM_URL     = /[\?#].*$/,
    _URL_RESOLVER = document.createElement("img");

var _QT_PLAYER = '\
<!--[if gt IE 6]>\n\
<object width="{WIDTH}" height="{HEIGHT+15}" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"><!\n\
[endif]--><!--[if !IE]><!-->\n\
<object width="{WIDTH}" height="{HEIGHT+15}" type="video/quicktime" data="{SRC}"><!--<![endif]-->\n\
<param name="src" value="{POSTER}" />\
<param name="href" value="{SRC}" />\
<param name="target" value="myself" />\
<param name="controller" value="{CONTROLS}" />\
<param name="enablejavascript" value="true" />\
<param name="autoplay" value="false" />\
<param name="autohref" value="{AUTOPLAY}" />\
<param name="kioskmode" value="false" />\
<param name="volume" value="{VOLUME}" />\
<param name="showlogo" value="false" />\
{FLASH_PLAYER}\
<!--[if gt IE 6]><!-->\n\
</object><!--<![endif]-->';

var _OGG_PLAYER = format('\
<applet code="com.fluendo.player.Cortado.class" archive="%1cortado.jar" width="{WIDTH}" height="{HEIGHT}">\
<param name="url" value="{SRC}">\
<param name="autoPlay" value="{AUTOPLAY}" />\
<param name="audio" value="{!MUTED}" />\
<param name="BufferSize" value="4096" />\
<param name="BufferHigh" value="25" />\
<param name="BufferLow" value="5" />\
</applet>',
jsb.host);

var _POSTER = '<a href="{SRC}"><img src="{POSTER}" width="{WIDTH}" height="{HEIGHT}" alt="download" /></a>';

// =========================================================================
// media/media.js
// =========================================================================

// An interface shared by audio/video.

var media = jsb.behavior.extend({
  NETWORK_EMPTY:     0,
  NETWORK_IDLE:      1,
  NETWORK_LOADING:   2,
  NETWORK_LOADED:    3,
  NETWORK_NO_SOURCE: 4,
  
  HAVE_NOTHING:      0,
  HAVE_METADATA:     1,
  HAVE_CURRENT_DATA: 2,
  HAVE_FUTURE_DATA:  3,
  HAVE_ENOUGH_DATA:  4,

  src: "",
  autobuffer: false,

  currentTime: 0,
  defaultPlaybackRate: 0,
  playbackRate: 0,
  autoplay: false,
  loop: false,
  
  controls: false,
  volume: 1,
  muted: false,
  
  get: function(element, propertyName) {
    switch (propertyName) {
      case "error":
        return null;
        
      case "currentSrc":
        return "";
        
      case "networkState":
        return this.NETWORK_EMPTY;
        
      case "buffered":
        return null;
        
      case "readyState":
        return this.HAVE_NOTHING;
        
      case "seeking":
        return false;
        
      case "startTime":
        return 0;
        
      case "duration":
        return 0;

      case "paused":
        return false;

      case "played":
        return null;

      case "seekable":
        return null;

      case "ended":
        return false;
    }
    
    return this.base(element, propertyName);
  },

  canPlayType: function(type) {
    if (_PROBABLY[type]) return "probably";
    if (_MAYBE[type]) return "maybe";
    var mimeTypes = navigator.mimeTypes;
    if (mimeTypes) {
      var mimeType = mimeTypes[type];
      if (mimeType && mimeType.enabledPlugin) return "maybe";
    }
    return "";
  },

  load: Undefined,
  pause: Undefined,
  play: Undefined
});

// =========================================================================
// media/audio.js
// =========================================================================

html5.audio = media.extend({
  // TO DO
});

// =========================================================================
// media/video.js
// =========================================================================

// Thanks to:
//  http://camendesign.com/code/video_for_everybody

html5.video = media.extend({
  width: 300,
  height: 150,
  poster: "",
  
  oncontentready: function(element) {
    // Don't bother initialising other video elements if installing Flash.
    if (document.getElementById("html5-flash-installer")) return;
    
    var style    = element.style,
        src      = this.get(element, "currentSrc"),
        ext      = src.replace(_TRIM_URL, ""),
        poster   = this.get(element, "poster"),
        autoplay = this.get(element, "autoplay"),
        muted    = this.get(element, "muted"),
        width    = this.get(element, "width"),
        height   = this.get(element, "height"),
        template = _USE_FLASH_PLAYER.test(ext) ? _QT_PLAYER : _USE_OGG_PLAYER.test(ext) ? _OGG_PLAYER : _POSTER;

    // Hide the control whilst writing to it.
    if (autoplay) style.visibility = "hidden";
    
    // Create the content.
    element.innerHTML += template
      .replace(/{FLASH_PLAYER}/, _flashVersion > 5 && _flashVersion < 9 ?
        _FLASH_INSTALLER :
        _FLASH_PLAYER.replace(/{VARS}/g,
          _FLASH_VARS
            .replace("{SRC}", _ENCODER.exec(src))
            .replace("{POSTER}", _ENCODER.exec(poster))))
      .replace(/{SRC}/g, src)
      .replace(/{POSTER}/g, poster)
      .replace(/{AUTOPLAY}/g, autoplay)
      .replace(/{CONTROLS}/g, this.get(element, "controls"))
      .replace(/{LOOP}/g, this.get(element, "loop"))
      .replace(/{!MUTED}/, !muted)
      .replace(/{MUTED}/g, muted)
      .replace(/{VOLUME}/g, this.get(element, "volume") * 100)
      .replace(/{WIDTH}/g, width)
      .replace(/{HEIGHT(\+\d+)?}/g, function(match, adjust) {
        return height + (parseInt(adjust) || 0);
      });
    // TO DO: check the spec for this behavior -@DRE
    if (width) style.width = width + "px";
    if (height) style.height = height + "px";

    // Create a small delay before re-showing the control.
    // This fixes a bug in the QT player in MSIE.
    if (autoplay) setTimeout(function() {
      style.visibility = "";
    }, 15);
  },

  get: function(element, propertyName) {
    switch (propertyName) {
      case "currentSrc":
        var src = this.get(element, "src");
        // Prefer MP4 (supportable by flash and <object>).
        // (OGG is only supportable with Java).
        if (!_USE_QT_PLAYER.test(src.replace(_TRIM_URL, ""))) {
          var source = this.querySelector(element, "source[type$=mp4]") ||
                       this.querySelector(element, "source[type$=ogg]");
          if (source) src = this.getAttribute(source, "src");
        }
        if (!src) return "";
        
        // Use a script element to resolve URLs.
        _URL_RESOLVER.src = src;
        return _URL_RESOLVER.src;
        
      case "videoWidth":
        return 0;

      case "videoHeight":
        return 0;
    }

    return this.base(element, propertyName);
  }
});

// =========================================================================
// media/flash.js
// =========================================================================

// Uses JW Player: http://www.longtailvideo.com/players/jw-flv-player/

// http://developer.longtailvideo.com/trac/wiki/FlashVars
var _ENCODER = new RegGrp({
  "\\?":   "%3F",
    "=":   "%3D",
    "&":   "%26"
});

var _FLASH_VARS = jsb.host +
  "player.swf?autostart={AUTOPLAY}&amp;repeat={LOOP}&amp;volume={VOLUME}&amp;mute={MUTED}&amp;image={POSTER}&amp;file={SRC}";

var _FLASH_PLAYER = '\
<object width="{WIDTH}" height="{HEIGHT+20}" type="application/x-shockwave-flash" data="{VARS}">\
<param name="movie" value="{VARS}">\
</object>';

var _FLASH_INSTALLER = format('\
<object id="html5-flash-installer" type="application/x-shockwave-flash" data="%1" width="{WIDTH}" height="{HEIGHT}">\
<param name="movie" value="%1">\
</object>',
  format("%1expressInstall.swf?MMredirectURL=%2&amp;MMplayerType=%3&amp;MMdoctitle=%4",
    jsb.host, _ENCODER.exec(location.href), detect.MSIE ? "ActiveX" : "PlugIn", _ENCODER.exec(document.title)));
      
var _flashVersion = 0;
/*@if (@_jscript)
try {
  _flashVersion = new ActiveXObject("ShockwaveFlash.ShockwaveFlash").GetVariable("$version").match(/\d+/)[0] - 0;
} catch(x) {
  for (var i = 9; i > 5; i--) {
    try {
      new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
      _flashVersion = i;
      break;
    } catch(x){}
  }
}
@else @*/
try {
  _flashVersion = navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin.description.match(/\d+/)[0] - 0;
} catch(x){}
/*@end @*/

}; ////////////////////  END: CLOSURE  /////////////////////////////////////
